import React, { Component } from 'react'
import { Vote, SelectItem } from "../types/vote"
import Selectitem from "./selecttem"
interface Iprops {
    item: Vote
}
export class voteitem extends Component<Iprops, {}> {

    render() {
        let { title, con, voteUsers, items, createUser, endTime } = this.props.item;

        let curUser: any = localStorage.getItem('username')
        //是否投过票
        let status: boolean = voteUsers.includes(curUser)
        //总票数
        let total = items.reduce((pre: number, cur: SelectItem) => {
            return pre + cur.num
        }, 0)
        return (
            <div className="voteitem">
                <h4>标题：{title}</h4>
                <span>描述：{con}</span>
                <div className="select">
                    {items.length && items.map((item: SelectItem) => <Selectitem key={item.id} total={total} item={item} status={status}></Selectitem>)}
                </div>
                <div>
                    <span>已经有{voteUsers.length}人参加</span>---
                    <span>{createUser} 创建</span>---
                    <span>{endTime}</span>
                </div>
            </div>
        )
    }
}

export default voteitem
